package com.bwie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.poi.StringUtils;
import com.bwie.pojo.TbBrand;
import com.bwie.service.TbBrandService;
import com.bwie.mapper.TbBrandMapper;
import com.bwie.utils.R;
import com.bwie.utils.TokenUtils;
import com.bwie.vo.PageInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
* @author Lenovo
* @description 针对表【tb_brand(品牌表)】的数据库操作Service实现
* @createDate 2025-06-20 18:37:50
*/
@Service
public class TbBrandServiceImpl extends ServiceImpl<TbBrandMapper, TbBrand>
    implements TbBrandService{

    @Autowired
    private TbBrandMapper tbBrandMapper;

    @Autowired
    private HttpServletRequest request;

    @Override
    public R getBrandList(PageInfoVo pageInfoVo) {
        Page<TbBrand> page = new Page<>(pageInfoVo.getPageNum(), pageInfoVo.getPageSize());
        LambdaQueryWrapper<TbBrand> wrapper = new QueryWrapper<TbBrand>().lambda().orderByDesc(TbBrand::getCreateTime);
        if(StringUtils.isNotEmpty(pageInfoVo.getKeyWord())){
            wrapper.like(TbBrand::getBrandName, pageInfoVo.getKeyWord());
        }
        Page<TbBrand> tbBrandPage = tbBrandMapper.selectPage(page, wrapper);
        return R.ok().setData(tbBrandPage);
    }

    @Override
    public R addBrandList(TbBrand tbBrand) {
        TbBrand tbBrand1 = tbBrandMapper.selectOne(new QueryWrapper<TbBrand>().lambda().eq(TbBrand::getBrandName, tbBrand.getBrandName()));
        if(tbBrand1!=null){
            return R.error().setMsg("该品牌已存在");
        }
        String token = request.getHeader("token");
        if(token==null){
            return R.error().setMsg("用户未登录");
        }
        String userName = TokenUtils.getUserName(token);
        tbBrand.setCreateBy(userName);
        tbBrand.setCreateTime(new Date());
        tbBrandMapper.insert(tbBrand);

        return R.ok();
    }

    @Override
    public R updateBrandStatus(TbBrand tbBrand) {
        TbBrand brand = tbBrandMapper.selectById(tbBrand.getBrandId());
        if(brand==null){
            return R.error().setMsg("该品牌不存在");
        }
        String token = request.getHeader("token");
        if(token==null){
            return R.error().setMsg("用户未登录");
        }
        String userName = TokenUtils.getUserName(token);
        brand.setUpdateBy(userName);
        brand.setUpdateTime(new Date());
        brand.setBrandStatus(tbBrand.getBrandStatus());
        tbBrandMapper.updateById(brand);

        return R.ok();
    }

    @Override
    public R deleteBrand(Integer brandId) {
        tbBrandMapper.deleteById(brandId);
        return R.ok();
    }

    @Override
    public R deleteBatchBrand(List<TbBrand> tbBrandList) {
        for (TbBrand tbBrand : tbBrandList) {
            tbBrandMapper.deleteById(tbBrand);
        }
        return R.ok();
    }

    @Override
    public R getBrandData() {
        List<TbBrand> tbBrands = tbBrandMapper.selectList(null);
        return R.ok().setData(tbBrands);
    }
}




